<?php
/**
 * Created by PhpStorm.
 * User: MACHENIKE
 * Date: 2017/7/11
 * Time: 17:24
 */
namespace Server;
use Org\Payment\sign;
use Pay\Payment_request;
use Pay\Payment_select;
class PayService {
    /**
     * 公众号支付接口
     * @param $num 价格
     * @param $price 金额
     * @param $redirect_url 回调地址
     * @return array
     */
    public static function openidPay($order_num, $amount_money, $total_money, $redirect_url) {
        $sign = sign::make(new Payment_request($order_num,$amount_money,$total_money, $redirect_url));
//        dump($sign);die();
        vendor('wechat-demo-php.sample');
        $test = new \import_merchant();
        $result = $test->wxOPCode($sign);
        if($result['code'] == 10000) {
            return ['code'=>0,'message'=>'success','data'=>self::returnHandle($result['data'])['token']];
        } else {
            return ['code'=>1,'message'=>$result['message'],'data'=>[]];
        }
    }
    /**
     * rsa验签
     * @param $data 待验签数据
     * @param $check_data 要校对的的签名结果
     * @param string $public_key_path 公钥地址
     * @return bool
     */
    public static function check_data($data,$check_data,$public_key_path = __ROOT__.'rsa_public_key.pem') {
        $data = http_build_query($data);
        $data = sha1($data);
        return rsaVerify($data,$public_key_path, $check_data);
    }
    /**
     * rsa验签
     * @param $data 待验签数据
     * @param $check_data 要校对的的签名结果
     * @param string $public_key_path 公钥地址
     * @return bool
     */
    public static function xly_check_data($data,$check_data,$public_key_path = __ROOT__.'xlyverify.pem')
    {
        $data = http_build_query($data);
        $data = urldecode($data);
        $data = sha1($data);
        return rsaVerify($data,$public_key_path, $check_data);
    }
    /**
     * 微信订单查询
     * @param $order_num
     * @return array
     */
    public static function weChat_order_select($order_num)
    {
        $sign = sign::make(new Payment_select($order_num));
        vendor('wechat-demo-php.sample');
        $test = new \import_merchant();
        $result = $test->wxOPCode($sign);
        if($result['code'] == 100) {
            return ['code'=>0,'message'=>'success','data'=>self::returnHandle($result['data'])];
        } else {
            return ['code'=>1,'message'=>$result['message'],'data'=>$result];
        }
    }
    /**
     * 解析返回数据解析
     * @param $data 请求返回的数据
     * @return array
     */
    public static function returnHandle($data)
    {
        return json_decode(base64_decode(urldecode($data)), true);
    }
}